import sys,re,os
from PyQt5.QtWidgets import QApplication,QWidget,QLabel,QPushButton,QComboBox,QVBoxLayout

class WifiHoolWin(QWidget):
  def __init__(self):
    super().__init__()
    self.initUI()
  
  # 初始化界面
  def initUI(self):
    self.ssidLabel = QLabel('制作者：Kevin\n使用方法，选择一个WiFi热点后，再点击获取密码按钮')
    self.showLabel = QLabel('正在获取...<h6 style="color:red;">请先选择一个热点再点击按钮</h6>')
    self.showKeyLabel = QLabel()

    # 按钮
    self.getKeyBtn = QPushButton("获取密码")
    self.getKeyBtn.setEnabled = False
    self.getKeyBtn.clicked.connect(self.showKey)

    # 实现下拉框
    self.combo = QComboBox()
    name = self.getId()
    for n in name:
      self.combo.addItem(n)
    self.combo.activated[str].connect(self.getLabel)
    self.combo.activated[str].connect(self.getKey)

    mainlayout = QVBoxLayout()
    mainlayout.addWidget(self.ssidLabel)
    mainlayout.addWidget(self.combo)
    mainlayout.addWidget(self.showLabel)
    mainlayout.addWidget(self.getKeyBtn)
    mainlayout.addWidget(self.showKeyLabel)

    self.setLayout(mainlayout)
    self.setFixedSize(400,240)
    self.setWindowTitle("WiFi密码获取器")

  # 获取WiFi的ssid列表
  def getId(self):
    a=os.popen('netsh wlan show profiles')
    b=a.read()
    c=re.findall('所有用户配置文件 : (.*?)\n    所有用户配置文件',b,re.S) 
    return c

  # 修改显示的WiFi名
  def getLabel(self,text):
    self.getKeyBtn.setEnabled = True
    self.showLabel.setText("确认选中: "+"<h2 style='color:red;'>"+text+"</h2>")
    self.showLabel.adjustSize()

  def getKey(self,text):
    a=os.popen('netsh wlan show profiles '+text+' key=clear')
    b=a.read()
    c=re.findall('关键内容            : (.*?)\n\n费用设置',b,re.S)
    self.t = c
  
  def showKey(self):
    self.showKeyLabel.setText("<h2 style='color:red;'>"+str(self.t[0])+"</h2>")
    self.showKeyLabel.adjustSize()

if __name__ == "__main__":
  app = QApplication(sys.argv)
  wifi = WifiHoolWin()
  wifi.show()
  sys.exit(app.exec_())